package com.qf.controller;

import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.service.GoodsProductService;
import com.qf.service.GoodsService;
import com.qf.service.OrderService;
import com.qf.service.UserService;
import com.qf.util.ResponseUtil;
import com.qf.vo.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

@RestController
@RequestMapping("/dashboard")
@CrossOrigin("*")
public class indexController {
    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsProductService goodsProductService;

    @GetMapping("")
    //具有管理员, 商品模块, 查询权限才可以访问
    @RequiresPermissions("admin:goods:list")
    //具有, 系统管理, 或者商品管理, 或者订单管理的查询权限, 才能够访问这个方法
    @RequiresPermissionsDesc(menu = {"系统管理", "商品管理", "订单管理"}, button = "查询")
    public Object dashboard(){
        int countUser = userService.countUser();
        int countOrder = orderService.countOrder();
        int countGoods = goodsService.countGoods();
        int countGoodsProduct = goodsProductService.countGoodsProduct();
        Map<String,Object> data=new HashMap<>();
        data.put("goodsTotal",countGoods);
        data.put("userTotal",countUser);
        data.put("productTotal",countGoodsProduct);
        data.put("orderTotal",countOrder);
        return ResponseUtil.ok(data);
    }
    @RequestMapping("/chart")
    public Object chart(){
        List<DayStatis> userCound = userService.incrCound();
        List<DayStatis> orderCount = orderService.incrCound();
        List<CategorySellAmts> categorySellAmts = orderService.categoryCount();

        CategorySellVo categorySellVo = fetchCategorySell(categorySellAmts);
        String[] dayData = unionDayData(userCound, orderCount);
        OrderAmtsVo orderAmtsVo = unOrderVo(orderCount);

        UserOrderCntVo userOrderCntVo = new UserOrderCntVo();
        userOrderCntVo.setDayData(dayData);
        userOrderCntVo.setOrderCnt(unCnt(dayData,orderCount));
        userOrderCntVo.setUserCnt(unCnt(dayData,userCound));

        Map<String,Object> data=new HashMap<>();
        data.put("categorySell",categorySellVo);
        data.put("orderAmts",orderAmtsVo);
        data.put("userOrderCnt",userOrderCntVo);

        return ResponseUtil.ok(data);
    }
    public int[] unCnt(String[] dayData,List<DayStatis> dayStatis){
        int[] cnt=new int[dayData.length];
        for (int i = 0; i < dayData.length; i++) {
            int data=0;
            for (DayStatis dayStati : dayStatis) {
                if (dayData[i].equals(dayStati.getDayStr())){
                    data=dayStati.getCnts();
                    break;
                }
            }
            cnt[i]=data;

        }
        return cnt;
    }
    public OrderAmtsVo unOrderVo(List<DayStatis> orderCount){
        OrderAmtsVo vo = new OrderAmtsVo();
        int size=0;
        if (orderCount!=null||orderCount.size()>0){
            size=orderCount.size();
        }
        String[] data=new String[size];
        int[] orderData=new int[size];
        BigDecimal[] bigDecimal=new BigDecimal[size];

        for (int i=0;i<size;i++){
            data[i]=orderCount.get(i).getDayStr();
            orderData[i]=orderCount.get(i).getCnts();
            bigDecimal[i]=orderCount.get(i).getAmts();
        }
        vo.setOrderAmtData(bigDecimal);
        vo.setDayData(data);
        vo.setOrderCntData(orderData);
        return vo;
    }
    public String[] unionDayData(List<DayStatis> userCound, List<DayStatis> orderCount){
        Set<String> date=new HashSet<>();
        for (DayStatis dayStatis : userCound) {
            date.add(dayStatis.getDayStr());
        }
        for (DayStatis dayStatis : orderCount) {
            date.add(dayStatis.getDayStr());
        }
        List<String> date1 = new ArrayList<>(date);
        Collections.sort(date1);
        return date1.toArray(new String[0]);
    }
    public CategorySellVo fetchCategorySell(List<CategorySellAmts> categorySellAmts){
        CategorySellVo vo = new CategorySellVo();
        if (categorySellAmts==null||categorySellAmts.size()==0){
            return vo;
        }
        String[] category=new String[categorySellAmts.size()];
        for (int i=0;i<categorySellAmts.size();i++){
            category[i]=categorySellAmts.get(i).getName();
        }
        vo.setCategoryNames(category);
        vo.setCategorySellData(categorySellAmts);
        return vo;
    }
}
